using ERP.ERPModels;
using ERP.ERPModels.EquipmentManage;
using ERP.ERPModels.HumanResources.HumanResourceManagement;
using ERP.ERPModels.HumanResources.MasterData;
using ERP.ERPModels.Production.MasterData;
using ERP.ERPModels.Production.ProductionManagement;
using ERP.ERPModels.QualityAssurance;
using ERP.ERPModels.RepositoryManage;
using ERP.ERPModels.WarehouseManagement;
using ERP.ERPModels.WorkforceManage;
using ERP.RBAC;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using ERP.ERPModels.WarehouseManagement;
using ERP.ERPModels.OrderManage;


namespace ERP.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class ERPDbContext :
    AbpDbContext<ERPDbContext>,
    ITenantManagementDbContext,
    IIdentityDbContext
{

    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    public DbSet<SaleSendOrderDetail> SaleSendOrderDetail { get; set; }
    public DbSet<SaleOrder> SaleOrder { get; set; }
    public DbSet<SaleSendOrder> SaleSendOrder { get; set; }
    public DbSet<Supplier> Supplier { get; set; }
    public DbSet<Procurement> Procurement { get; set; }
    public DbSet<ProcurementDetail> ProcurementDetail { get; set; }
    public DbSet<Team> Team { get; set; }
    public DbSet<TeamMember> TeamMember { get; set; }
    public DbSet<Repair> Repair { get; set; }
    public DbSet<Warehouse> Warehouse { get; set; }
    public DbSet<Dvsubject> Dvsubject { get; set; }
    public DbSet<Pendingtasks> Pendingtasks {  get; set; }
    public DbSet<Salesout> Salesout { get; set; }

    public DbSet<SalesoutDetail> SalesoutDetail {  get; set; }
    public DbSet<HolidaySettingModel> HolidaySettingModel {  get; set; }
    public DbSet<EquipmentMaintenancePlan> EquipmentMaintenancePlan { get; set; }
    public DbSet<EquipmentMaintenancePlanDvsubject> EquipmentMaintenancePlanDvsubject { get; set; }
    public DbSet<ProductionReturn> ProductionReturn { get; set; }
    public DbSet<ProductionReturnDetail> ProductionReturnDetail { get; set; }
    public DbSet<ProductionScheduling> ProductionScheduling { get; set; }
    public DbSet<ProductionWorkOrders> ProductionWorkOrders { get; set; }
    public DbSet<CustomerOrders> CustomerOrders { get; set; }
    public DbSet<CustomerOrdersDetail> CustomerOrdersDetail { get; set; }
    public DbSet<Inventory> Inventory { get; set; }
    public DbSet<Customers> Customers { get; set; }
    public DbSet<Material> Material { get; set; }
    public DbSet<MaterialType> MaterialType { get; set; }

    public DbSet<MaterialClass> MaterialClass { get; set; }
    public DbSet<Packing> Packing { get; set; }
    public DbSet<PackingDetail> PackingDetail { get; set; }
    public DbSet<Workshop> Workshop { get; set; }


    public DbSet<PositionLevel> PositionLevels { get; set; }
    public DbSet<Menu> Menus { get; set; }
    public DbSet<Roles> Roless { get; set; }
    public DbSet<Users> Userss { get; set; }
    public DbSet<Permissions> Permissions { get; set; }
    public DbSet<RolePermission> RolePermission { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }

    public DbSet<CustomerManagement> CustomerManagement { get; set; }


    public DbSet<InterviewControl> InterviewControl {  get; set; }

    public DbSet<RolePermission> RolePermissions { get; set; }

    public DbSet<Outsourcing> Outsourcing { get; set; }

    #region Entities from the modules

    /* Notice: We only implemented IIdentityProDbContext and ISaasDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityProDbContext and ISaasDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    // Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }
    public DbSet<ProductionDispatch> ProductionDispatch { get; set; }
    public DbSet<InventoryTransaction> InventoryTransaction { get; set; }
    #endregion 
    
    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }


    public DbSet<JobApplication> JobApplication { get; set; }

    // 工序配置
    public DbSet<ProcessConfiguration> ProcessConfiguration { get; set; }
    /// <summary>
    /// 排班计划
    /// </summary>
    public DbSet<SchedulingPlan> SchedulingPlan { get; set; }
    /// <summary>
    /// 班次实体
    /// </summary>
    public DbSet<Classes> Classes { get; set; }

    #region 招聘系统
    public DbSet<TalentRegistration> TalentRegistration { get; set; }
    #endregion

    #region 生产管理
    public DbSet<ProductionWaste> ProductionWastes { get; set; }
    #endregion

    #region 质量管理
    /// <summary>
    /// 检测项设置
    /// </summary>
    public DbSet<Testitemsettings> Testitemsettings { get; set; }
    #endregion

    public ERPDbContext(DbContextOptions<ERPDbContext> options)
        : base(options)
    {

    }


    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureFeatureManagement();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureTenantManagement();
        builder.ConfigureBlobStoring();

        /* Configure your own tables/entities inside here */

        // 配置ProductionWaste实体映射到ProductionWaste表（而不是默认的ProductionWastes）
        builder.Entity<ProductionWaste>(b =>
        {
            b.ToTable("ProductionWaste");
        });

        // 配置CustomerManagement实体映射到CustomerManagement表（而不是默认的CustomerManagements）
        builder.Entity<CustomerManagement>(b =>
        {
            b.ToTable("CustomerManagement");
        });
        
        // 配置SchedulingPlan实体
        builder.Entity<SchedulingPlan>(b =>
        {
            b.ToTable("SchedulingPlan");
            b.Property(p => p.TeamIdsStr).HasColumnName("TeamIdsStr").IsRequired(false);
            b.Ignore(p => p.TeamIds); // 忽略非映射属性
        });
        
        // 配置Classes实体
        builder.Entity<Classes>(b =>
        {
            b.ToTable("Classes");
            b.Property(p => p.SerialNumber).IsRequired();
            b.Property(p => p.ClassesName).IsRequired().HasMaxLength(50);
            b.Property(p => p.StartTime).IsRequired();
            b.Property(p => p.EndTime).IsRequired();
        });
    }
}
